<?php namespace app\admin\controller;

use system\model\Config;
use system\model\Keyword;

class Message {
	public function __construct() {
		User::isLogin();
	}

	//发表关键词
	public function post() {
		$module = Request::get( 'm' );
		$class  = 'module\\' . $module . '\Message';
		$obj    = new $class;
		//判断编辑
		$ekid=Request::get('kid');
		$keyword            = new Keyword();
		if ( IS_POST ) {
			//判断关键字不能重复
			if (!$ekid && $same=$keyword->where('keyword',Request::post( 'keyword' ))->first()) {
				message( '关键字不能重复添加', u( 'lists', [ 'm' => $_GET['m'] ] ), 'error' );
			}
			//回复内容不能为空
			if (Request::post('content')=='[]') {
				message( '回复内容不能为空', u( 'lists', [ 'm' => $_GET['m'] ] ), 'error' );
			}
			$keyword['kid']=$ekid;
			$keyword['keyword'] = Request::post( 'keyword' );
			$keyword['module']  = $module;
			$kid                = $keyword->save();
			if ($ekid) {
				$kid=$ekid;
			}
			$obj->submit( $kid );
			message( '保存成功', u( 'lists', [ 'm' => $_GET['m'] ] ), 'success' );
		}

		if ($ekid) {
			$data=$keyword->find($ekid);
			$answer=$obj->getData($ekid);
			View::with('answer',$answer);
			View::with('data',$data);
		}

		View::with( 'messageField', $obj->SonDisplay() );

		return view();
	}

	//显示消息列表
	public function lists() {
		$module = Request::get( 'm' );
		$data   = Db::table( 'keywords' )->where( 'module', $module )->paginate( 5 );

		return view()->with( 'data', $data );
	}

	//系统消息处理
	public function system() {
		if ( IS_POST ) {
			$model                    = new Config();
			$model['id']              = 1;
			$model['default_message'] = Request::post( 'content' );
			$model->save();
			message( '保存成功', 'refresh', 'success' );
		}

		return view();
	}
}